package com.AAL.Action;

import com.AAL.Utils.*;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.AAL.Model.Account;
import com.AAL.Model.Order;
import com.AAL.Model.UpdatingOrder;
import com.AAL.Model.Employee;
import com.AAL.DAO.AccountsDAO;
import com.AAL.DAO.EmployeeDAO;
import com.AAL.DAO.OrderDAO;
import com.AAL.DAO.UpdatingOrderDAO;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UpdatingOrderAction extends ActionSupport implements SessionAware {

	private Map<String, Object> session;
	private Account account;
	private AccountsDAO lnkAccount;
	private EmployeeDAO lnkEmployee;
	private int idOrder;
	private UpdatingOrder updateingorder;
	private UpdatingOrderDAO lnkUpdatingOrder;
	Employee employee;
	OrderDAO lnkOrder;

	public UpdatingOrderAction() {
		updateingorder = new UpdatingOrder();
		lnkOrder = new OrderDAO();
		lnkEmployee = new EmployeeDAO();
		lnkUpdatingOrder = new UpdatingOrderDAO();
		this.lnkAccount = new AccountsDAO();
	}

	//Thu 5 ngay 31/3/2011
	//Huyen
	//Form yeu cau cap nhat don hang
	public String execute() {
		try {
			System.out.print("Bat dau ham");
			HttpServletRequest request = (HttpServletRequest) ActionContext
					.getContext().get(ServletActionContext.HTTP_REQUEST);
			int Id = (Integer.parseInt(request.getParameter("id")));
			System.out.print("\nID" + Id);
			updateingorder.setOrder(lnkOrder.getOrder(Id));
			System.out.print("\nAAAAAAAAAOrderID:"
					+ updateingorder.getOrder().getOrderID());

			Account myAccount = null;
			if (session.get("myAccount") != null)
				myAccount = (Account) session.get("myAccount");

			System.out.print("Den day");
			if (myAccount != null) {
				System.out.print("Bat dau if");
				// int EmployeeID=myAccount.getEmployee().getEmployeeID();
				// updateingorder.setEmployee(lnkEmployee.getEmployee(EmployeeID));
				updateingorder.setEmployee(myAccount.getEmployee());
				System.out.print("\nAAAAAAAAAEmaplyeename:"
						+ updateingorder.getEmployee().getAccount()
								.getUserName());
			}
			updateingorder.setWritingDate(DateUtil.getToday());
			updateingorder.setTitle("");
			updateingorder.setContents("");
			updateingorder.setIsAccpetByDealer(false);
			session.put("updatingOrderSes", updateingorder);
			System.out.print("Ket thuc ham");

		} catch (Exception e) {
			// System.out.println("E:"+ e.getMessage());
		}
		return SUCCESS;

	}

	//Thu 5 ngay 31/3/2011
	//Huyen
	//Luu yeu cau cap nhat don hang
	public String Save() {
		try {
			System.out.println("Bat dau ham Save");
			if (session.get("updatingOrderSes") != null) {
				UpdatingOrder updateingorder1 = (UpdatingOrder) session
						.get("updatingOrderSes");			
				updateingorder.setOrder(updateingorder1.getOrder());
				updateingorder.setEmployee(updateingorder1.getEmployee());
				updateingorder.setIsAccpetByDealer(updateingorder1.getIsAccpetByDealer());
				updateingorder.setWritingDate(updateingorder1.getWritingDate());
				updateingorder = lnkUpdatingOrder
						.saveAndUpdateUpdatingOrder(updateingorder);
				if (updateingorder == null) {
					addActionError("Không thể thêm");
					return ERROR;
				} else {
					System.out.println("da them");
				}
				System.out.println("Ket thuc ham Save");
			}
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public int getIdOrder() {
		return idOrder;
	}

	public void setIdOrder(int idOrder) {
		this.idOrder = idOrder;
	}

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public UpdatingOrder getUpdateingorder() {
		return updateingorder;
	}

	public void setUpdateingorder(UpdatingOrder updateingorder) {
		this.updateingorder = updateingorder;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

}
